#ifndef __AIO_FUN_H
#define __AIO_FUN_H

#define DIPSW3_IN       (~inport(0x05))
#define DIPSW8_IN       (~inport(0x04)&0x0F)
#define DIPSW9_IN       ((~inport(0x04)>>4)&0x0F)

#define DIH1_IN         (inport(0x07))
#define DIH2_IN         (inport(0x08))
#define DIH3_IN         (inport(0x0D))
#define DIH4_IN         (inport(0x0C))
#define DIH5_IN         (inport(0x0E))
#define DIH6_IN         (inport(0x09))

#define DOH1_IN         (inport(0x00))
#define DOH2_IN         (inport(0x01))
#define DOH3_IN         (inport(0x02))

#define LCD_KEY_IN()    (inport(0x0B))
#define LCD_BUSY_IN()   (inport(0x03))
#define LCD_DATA_OUT(x) (outport(0x03,x))

#define DIH_DETECT_UNIT    1   // ms
#define DIH_BUF_NUM      250
#define DIH_DB_DLY_MAX    64
#define DIH_DB_DLY_DEF    16   
#define DIH_DB_DLY_MIN     2


#ifdef FIRST
#define EXTERN 
U8 DIH_DB_dly=DIH_DB_DLY_DEF;
#else
#define EXTERN  extern
extern U8  DIH_DB_dly;
#endif
EXTERN U16 aio_dly;

#define  MAX_DI_NUM  1024
#define  MAX_DI_BYTE  (MAX_DI_NUM/8)
#define  MAX_DO_NUM   512
#define  MAX_DO_BYTE  (MAX_DO_NUM/8)
#define  MAX_AI_NUM   384
#define  MAX_AI_BYTE  (MAX_AI_NUM/8)
#define  MAX_CI_NUM    8
#define  MAX_CI_BYTE  (MAX_CI_NUM/8)
#define  MAX_AO_NUM   (MAX_AI_NUM)
#define  MAX_AO_BYTE  (MAX_AO_NUM/8)

#define DI_NUM    (MAX_DI_NUM)
#define DI_BNUM   (MAX_DI_NUM/8)
EXTERN U8  DICHGC[MAX_DI_NUM];
EXTERN U8  DI_STUS[DI_BNUM],DI_OSTUS[DI_BNUM];
EXTERN U8  DI_change;

void  DI_simu_soe(int ch,int stus,int ofsms);
void  DI_set_simu(U16,U8,U8);
void  DI_set_bit(U16,U8,U8,U8);
char  DI_get_bit(U16 ch);
void  DI_set_stus(U16 ch,U8 stus,U8 mcd);

#define DIH_NUM     48  //real=48
#define DIH_BNUM     6

EXTERN U8  DIH_STUS[DIH_BNUM];
EXTERN U8  DIH_buf_h,DIH_buf_t,DIH_buf_maxc,DIH_buf_rdy;;
EXTERN U8  DIH_BUF[DIH_BUF_NUM][DIH_BNUM];
void  DIH_DB_cw(void);
void  DIH_input(unsigned char *stus);

#define RSF_DIS     (DIH_NUM)
#define RSF_ALARM   (DIH_NUM)
#define RSF_NUM     16   //SM_NUM
#define RSF_BNUM     2

#define RSF_SYS_ALARM (RSF_ALARM)

#define AEC_DIS      (RSF_DIS+RSF_NUM)  // 64
#define AEC_DI_NUM   128

#define ID0_DI_NUM   (AEC_DIS+AEC_DI_NUM)
// DIH48+RSF16+[In64+OutU32+OutD32] = 192

#define EVNB_NUM  256
#ifdef EVNB_NUM

typedef struct { 
    S32 secs;
    U16 ms;
} DIH_BUF_DT;
EXTERN DIH_BUF_DT  DIH_DT[DIH_BUF_NUM];

typedef struct { 
    U8  hc,stus;
    U16 ch; 
    S32 secs;
    U16 iv_ms;
} EVNB_PARM;

EXTERN EVNB_PARM EVNB[EVNB_NUM];
EXTERN U16 EVNB_head;

void EVNB_clr(U16 *,U16 *);
void EVNB_tail_update(U16 *,U16 *);
void EVNB_tails_set(U16 *,U16 *);
int  EVNB_srdy(U16);
void EVNB_write(U8 hc,U16 ch,U8 stus,S32 secs,U16 iv_ms,S16 ms_ofs);
int  EVNB_sread(U16 *tails,U8 *hc,U16 *ch,U8 *stus,S32 *secs,U16 *iv_ms);
int  EVNB_i_srdy(U16 tails);
int  EVNB_i_sread(U16 *tails,U16 *ch,U8 *stus,U16 *yy,U8 *mn,U8 *dd,U8 *hh,U8 *mm,U8 *ss,U16 *ms);
int  ChgTime(U8 *rhh,U8 *rmm,U8 *rss,U16 *rms,S16 msofs);
#endif

#define DO_NUM    (MAX_DO_NUM)
#define DO_BNUM   (MAX_DO_NUM/8)
#define DOH_NUM   24  
#define DOH_BNUM   3

EXTERN U8 DO_STUS[DO_BNUM];
EXTERN U8 DOH_STUS[DOH_BNUM];
EXTERN U8 DO_OP_STUS[DO_BNUM];

#define CDO_BUF_NUM    64
#ifdef CDO_BUF_NUM
#define CDO_PULSE_MAX  5000  //ms
#define CDO_PULSE_DEF   500  
#define CDO_PULSE_MIN   200
U8    CDO_buf_rdy(U8 tail);
U8    CDO_buf_read(U8 *tail,U16 *ch,U8 *mode,U16 *pulse);
void  CDO_buf_write(U16 ch,U8 mode,U16 pulse);
void  DOH_output(unsigned char ch,unsigned char mode);

void DOH_IN(void); 
void DOH_OUT(unsigned char address,unsigned char data);

#define AEC_DOS      (DOH_NUM)  // 24
#define AEC_DO_NUM   160

#define ID0_DO_NUM   (AEC_DOS+AEC_DO_NUM)

#define CDO_INT_PRO
#ifdef CDO_INT_PRO
EXTERN char cdo_in_flag,cdo_out_flag,doh_out_c;
#endif

#endif
void DO_set_bit(U16 ch,U8 stus);
char DO_get_bit(U16 ch);

#define AI_NUM     (MAX_AI_NUM)
#define AI_BNUM    (MAX_AI_NUM/8)
EXTERN S16 AI_BUFF[MAX_AI_NUM];
EXTERN S32 AI_COUNT[MAX_AI_NUM];
void  AI_set_count(int ch,S32 fvar);
S32   AI_get_count(int ch);
S32   AI_get_ibuf(int ch);

//#define AIH_NUM   8  
#ifdef AIH_NUM
EXTERN S16  AD_BUFF[AIH_NUM];
#define AD_BAS_MAX 1.1
#define AD_BAS_MIN 0.9
EXTERN float AD_BAS[AIH_NUM];
#endif

#define AEC_AIS       0 // 0
#define AEC_AI_NUM   64

#define ID0_AI_NUM   (AEC_AIS+AEC_AI_NUM)

#define WAO_BUF_NUM  32
#ifdef WAO_BUF_NUM
U8    WAO_buf_rdy(U8 tail);
U8    WAO_buf_read(U8 *tail,U16 *ch,S32 *value,S16 *fc);
void  WAO_buf_write(U16 ch,S32 value,S16 fc);
#endif

#ifdef MAX_AO_NUM
#define AO_NUM    (MAX_AO_NUM)
#define AO_BNUM   (MAX_AO_NUM/8) 
EXTERN S16 AO_BUFF[MAX_AO_NUM];
EXTERN S32 AO_COUNT[MAX_AO_NUM];
void  AO_set_count(int ch,S32 fvar);
S32   AO_get_count(int ch);
S32   AO_get_ibuf(int ch);
#endif

#ifdef MAX_CI_NUM
#define CI_NUM    (MAX_CI_NUM)
EXTERN S32 CI_BUFF[MAX_CI_NUM];
EXTERN S32 CI_COUNT[MAX_CI_NUM];
void  CI_set_count(int ch,S32 fvar);
S32   CI_get_count(int ch);
S32   CI_get_ibuf(int ch);
#endif


void  aio_sec_pro(void);
void  aio_pro(void);
void  real_pro(void);
void  CDO_task(U16 ch,U16 cmode,U16 cpulse);
void  WAO_task(U16 ch,S32 aov,S16 fc);

#endif
